package com.example.dell.myapplication;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import static android.view.View.*;


public class FileStorageActivity extends AppCompatActivity {

    final String FILE_NAME = "memo.txt";
    boolean ischanged = false;

    ImageView openFileBnt,saveFileBnt,exitFileBnt;
    EditText editFileEdt,fielPathEdt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_storage);

        //初始化
        openFileBnt=(ImageView)findViewById(R.id.openFileBnt);
        saveFileBnt=(ImageView)findViewById(R.id.saveFileBnt);
        exitFileBnt=(ImageView)findViewById(R.id.exitFileBnt);
        editFileEdt=(EditText)findViewById(R.id.editFileEdt);
        fielPathEdt=(EditText)findViewById(R.id.fielPathEdt);

        //注册事件监听器
        openFileBnt.setOnClickListener(onClickListener);
        saveFileBnt.setOnClickListener(onClickListener);
        exitFileBnt.setOnClickListener(onClickListener);

        //设置是否变化
        editFileEdt.addTextChangedListener(textChangedListener);
    }
    private OnClickListener onClickListener=new View.OnClickListener(){
        public void onClick(View view){
            switch (view.getId()) {
                case R.id.openFileBnt:
                    readFile();
                    break;
                case R.id.saveFileBnt:
                    writeFile();
                    ischanged=false;
                    break;
                case R.id.exitFileBnt:
                    if(ischanged){
                        AlertDialog.Builder alterDialog=new AlertDialog.Builder(FileStorageActivity.this);
                        alterDialog.setTitle("提示")
                                .setMessage("文件有改动，是否保存")
                                .setPositiveButton("保存", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialogInterface,int i) {
                                        writeFile();
                                        finish();
                                    }
                                })
                                .setNegativeButton("不保存",new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialogInterface,int i) {
                                        finish();
                                    }
                                })
                                .create()
                                .show();
                    }else{
                        finish();
                        Toast.makeText(FileStorageActivity.this,"安全退出",
                                Toast.LENGTH_SHORT).show();
                    }
            }
        }
    };
    private void writeFile(){
        String str = editFileEdt.getText().toString();
        try {
            FileOutputStream fos= openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
            fos.write(str.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readFile() {
        try {
            FileInputStream fis = openFileInput(FILE_NAME);
            byte[] buff = new byte[1024];
            int hasRead = 0;
            StringBuilder sb = new StringBuilder();
            while ((hasRead = fis.read(buff)) > 0) {
                sb.append(new String(buff, 0, hasRead));
            }
            fis.close();
            editFileEdt.setText(sb);
            fielPathEdt.setText(getFilesDir().toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private TextWatcher textChangedListener = new TextWatcher() {
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        public void afterTextChanged(Editable editable) {
            ischanged = true;
        }
    };
}
